package com.dh.manager.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dh.dao.bo.EncryBO;
import com.dh.dao.dto.UserInfoDto;
import com.dh.dao.entity.UserInfo;
import com.dh.dao.entity.UserLogin;
import com.dh.dao.mapper.UserInfoMapper;
import com.dh.dao.mapper.UserLoginMapper;
import com.dh.dao.vo.ResponseUserInfoVo;
import com.dh.manager.redis.RedisUtils;
import com.dh.manager.service.LoginServer;
import com.dh.manager.util.JWTUtils;
import com.dh.manager.util.WxUtils;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

@Component
public class LoginServerImpl implements LoginServer {
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private UserLoginMapper userLoginMapper;
    @Resource
    private JWTUtils jwtUtils;
    @Resource
    private RedisUtils redisUtils;

    @Override
    @ApiModelProperty("解密，注册新用户")
    @Transactional(rollbackFor = {Exception.class,RuntimeException.class})
    public String createUser(EncryBO encryBO,String openId,String sessionKey) {
        //解密
        String encryptedData = encryBO.getEncryptedData();
        String iv = encryBO.getIv();
        String decrypt = WxUtils.decrypt(encryptedData, sessionKey, iv);
        //注册
        UserLogin userLogin = new UserLogin();
        userLogin.setOpenId(openId);
        String token = jwtUtils.createToken(openId);
        userLogin.setToken(token);
        int count = userLoginMapper.insert(userLogin);
        if(count!=1){
            throw new RuntimeException("login注册失败");
        }
        //解密用户信息
        JSONObject jsonObject = JSONObject.parseObject(decrypt);
        UserInfo userInfo=jsonObject.toJavaObject(UserInfo.class);
        userInfo.setOpenId(openId);
        int insert = userInfoMapper.insert(userInfo);
        if(insert!=1){
            throw new RuntimeException("info注册失败");
        }
        long redisResult = redisUtils.sSetAndTime("token",1800, token);
        if(redisResult!=1){
            throw new RuntimeException("缓存失败");
        }
        return token;
    }

    @Override
    @ApiModelProperty("用户登录获取token")
    @Transactional(rollbackFor = {Exception.class,RuntimeException.class})
    public String updateUser(UserLogin userLogin) {
        String token = jwtUtils.createToken(userLogin.getOpenId());
        userLogin.setToken(token);
        int updateById = userLoginMapper.updateById(userLogin);
        if(updateById!=1){
            throw new RuntimeException("更新失败");
        }
        long redisResult = redisUtils.sSetAndTime("token", 1800, token);
        if(redisResult!=1){
            throw new RuntimeException("缓存失败");
        }
        return token;
    }

    @Override
    @ApiModelProperty("包装视图对象")
    @Transactional(rollbackFor = {Exception.class,RuntimeException.class})
    public ResponseUserInfoVo packageVo(String openId,String token) {
        ResponseUserInfoVo responseUserInfoVo = new ResponseUserInfoVo();
        UserInfo userInfo = userInfoMapper.selectByOpenId(openId);
        if (Objects.isNull(userInfo)){
            throw new RuntimeException("查询失败");
        }
        UserInfoDto userInfoDto = new UserInfoDto();
        userInfoDto.setAvatarUrl(userInfo.getAvatarUrl());
        userInfoDto.setNickName(userInfo.getNickName());
        userInfoDto.setWangBi(userInfo.getWangBi());
        responseUserInfoVo.setUserInfoDto(userInfoDto);
        responseUserInfoVo.setToken(token);
        return responseUserInfoVo;
    }
}
